using System.Collections.Generic;
using System.Linq;

namespace SkynetMongoDBAccess;

public static class util
{
	public static string HandleLike(this string s)
	{
		while (s.ToLower().Contains("like"))
		{
			List<string> list = s.Split(' ').ToList();
			list.RemoveAll((string d) => d == "");
			string text = "";
			string text2 = "";
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].ToLower() == "like")
				{
					text = list[i - 1];
					text2 = list[i + 1];
					list.RemoveAt(i);
					list.Remove(text);
					list.Remove(text2);
					break;
				}
			}
			text2 = text2.Replace("'", "").Replace("%", ".*");
			string text3 = string.Concat(list.ToArray()).Trim('&');
			text3 += $"&& new RegExp('^{text2}$', 'gi').test({text})";
			s = text3;
		}
		return s.TrimStart('&');
	}
}
